package com.maserb.monitor.web.jw_report;

import com.maserb.monitor.common.AjaxResult;
import com.maserb.monitor.common.AjaxUtils;
import com.maserb.monitor.common.Func_T;
import com.maserb.monitor.model.JwFileInfoModel;
import com.maserb.monitor.service.JwReport.JwFileInfoService;
import com.maserb.monitor.service.common.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

//附件上传接口
@Controller
@RequestMapping("/jwFileApp")
@Slf4j
public class JwFileInfoAppController {
    @Autowired
    JwFileInfoService jwFileInfoService;

    @Autowired
    FileService fileService;

    @Value("${monitor.uploadDir}")
    private String uploadDir;


    public String getUploadDir() {
        return uploadDir;
    }


    //手机端上传文件到后台
    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult<JwFileInfoModel> uploadFile(MultipartHttpServletRequest muiltRequest,int type) {
        return AjaxUtils.process(new Func_T<JwFileInfoModel>() {
            @Override
            public JwFileInfoModel invoke() throws Exception {
                String fileName = muiltRequest.getFileNames().next(); //得到文件名（注意。是content-type 中的name="file1"，而不是真正的文件名）
                MultipartFile file = muiltRequest.getFile(fileName);   //得到该文件
                //图片
                if(type==1){
                    return jwFileInfoService.uploadImage(file);
                }
                //录音
                else if(type==2){
                    return jwFileInfoService.uploadRecording(file);
                }
                //视频
                else if((type==3)){
                    return jwFileInfoService.uploadVideo(file);
                }
                return jwFileInfoService.uploadRecording(file);
            }
        });
    }

    //手机端下载文件
    @RequestMapping("download")
    public String downLoad(HttpServletResponse response,String path){
        String filename = FilenameUtils.concat(getUploadDir(), path);
        File file = new File(filename);
        if(file.exists()){ //判断文件父目录是否存在
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" + filename);
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;
            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer);
                    i = bis.read(buffer);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----------file download" + filename);
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    /*@RequestMapping("/downloadImage")
    public ResponseEntity<byte[]> downloadImage(@RequestParam("filename") String filename) throws Exception {
        return jwFileInfoService.downloadImage(filename);
    }

    @RequestMapping("/testUpload")
    public String testUpload() {
        return "common/test_upload";
    }*/
}
